package com.health.home.service.lmpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.home.domain.po.TmedicineTypePo;
import com.health.home.domain.vo.TmedicineTypeVo;
import com.health.home.mapper.TmedicineTypeMapper;
import com.health.home.service.TmedicineTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xpz
 * @date 2024/12/12 1:09
 */
@Service
public class TmedicineTypeServicelmpl extends ServiceImpl<TmedicineTypeMapper, TmedicineTypePo> implements TmedicineTypeService{
    @Override
    public List<TmedicineTypePo> lists(TmedicineTypeVo tmedicineTypeVo) {

        LambdaQueryWrapper<TmedicineTypePo> wrapper = new LambdaQueryWrapper<>();
         if (StringUtils.isNotBlank(tmedicineTypeVo.getMedicineTypeName())){
             wrapper.eq(TmedicineTypePo::getMedicineTypeName,tmedicineTypeVo.getMedicineTypeName());
         }
        List<TmedicineTypePo> list = list(wrapper);
        return list;
    }

    private boolean isMedicineTypeNameUnique(String medicineTypeName) {
        LambdaQueryWrapper<TmedicineTypePo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TmedicineTypePo::getMedicineTypeName, medicineTypeName);
        // 如果查询到结果，则说明名称已存在
        int count = (int) count(wrapper);
        return count == 0;  // 如果返回 count > 0，表示名称已经存在
    }
    @Override
    public boolean saveMedicineType(TmedicineTypePo tmedicineTypePo) {
        // 首先检查药品类型名称是否唯一
        if (!isMedicineTypeNameUnique(tmedicineTypePo.getMedicineTypeName())){
            throw new RuntimeException("药品类型名称已经存在，请选择其他名称！");
        }
        //保存数据
        return save(tmedicineTypePo);
    }


}
